package com.ccx.coachsite.ui.custom;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.FrameLayout;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.bus.DateParams;
import com.ccx.coachsite.bus.ExpertExBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.ui.manage.ManageAddCustomResultView;
import com.ccx.coachsite.ui.manage.ManageAddCustomView;
import com.ccx.coachsite.ui.manage.ManageAddGroupView;
import com.ccx.coachsite.ui.manage.ManageCustomInfoView;
import com.ccx.coachsite.ui.manage.ManageCustomView;
import com.ccx.coachsite.ui.manage.ManageGroupView;
import com.ccx.coachsite.ui.manage.ManageKey;
import com.ccx.coachsite.ui.manage.ManageMessageView;
import com.ccx.coachsite.ui.manage.ManageSalaryQueryView;
import com.ccx.coachsite.ui.manage.ManageSalaryView;
import com.ccx.coachsite.ui.manage.MangeGroupInfoView;
import com.ccx.coachsite.ui.manage.MyManageView;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerMainBase;
import com.ccx.coachsite.ui.own.OwerViewKey;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class CustomMainView extends OwerMainBase{

	private MyCustomView mMyCustomView;
	private MyManageView mMyManageView;
	private ManageGroupView mManageGroupView;
	private ManageCustomView mManageCustomView;
	private ManageSalaryView mManageSalaryView;
	private ManageSalaryQueryView mManageSalaryQueryView;
	private MangeGroupInfoView mMangeGroupInfoView;
	private ManageAddGroupView mManageAddGroupView;
	private ManageCustomInfoView mManageCustomInfoView;
	private ManageAddCustomView mManageAddCustomView;
	private ManageAddCustomResultView mManageAddCustomResultView;
	private ManageMessageView mManageMessageView;
	
	public CustomMainView(Context context, int param) {
		super(context);
		isCustom = true;
		mContext = context;
		addMainView(R.layout.custom_main);
		rootLayout = (FrameLayout) findViewById(R.id.custom_main_frame);
		rootViewId = R.id.ll_custom_personal_info;
		initButton();
		SwitchView(R.id.ll_custom_personal_info);
		mMyCustomView = new MyCustomView(context, this);
	}
	
	public void refreshMessage(String user)
	{
		if(mManageMessageView != null)
		{
			mManageMessageView.refreshMessageData(user);
		}
	}
	
private ExpertExBus mExBus;
	
	public void loadInfo(int viewId)
	{
		if(R.id.ll_custom_personal_info == viewId)
		{
			if(mExBus != null)
				return;
			
			mExBus = new ExpertExBus(mContext, this);
			String mMobile = SharedPreferencesUtil.getValue(mContext, SharedKey.Shared_Mobile_Key, "");
			mExBus.loadData(mMobile);
		}
	}
	
	@Override
	public void onCompleted(boolean isSuccess, String desc) {
		super.onCompleted(isSuccess, desc);
		if(isSuccess)
		{
			if(R.id.ll_custom_personal_info == currentViewId)
			{
				mMyCustomView.onLoadSuccess();
			}
		}
		
		if(R.id.ll_custom_personal_info == currentViewId)
			mExBus = null;
		
	}
	
	public void onBackClick()
	{
		super.onBackClick();
		switch (currentViewId) {
		case ManageKey.ll_manage_custom:
		case ManageKey.ll_manage_group:
		case ManageKey.ll_manage_salary:
		case OwerViewKey.ll_personal_setting:
		case OwerViewKey.ll_personal_message:
		case OwerViewKey.ll_personal_friend:
		case OwerViewKey.ll_personal_collect:
		case OwerViewKey.ll_personal_share:
			PopViewById(currentViewId);
			SwitchView(R.id.ll_custom_personal_info);
			break;
		case OwerViewKey.ll_personal_more_download:
		case OwerViewKey.ll_personal_more_repassword:
		case OwerViewKey.ll_personal_more_score:
		case OwerViewKey.ll_personal_more_suggest:
			PopViewById(currentViewId);
			SwitchView(OwerViewKey.ll_personal_setting);
			break;
		case OwerViewKey.ll_personal_message_detail:
			PopViewById(currentViewId);
			SwitchView(OwerViewKey.ll_personal_message);
			break;
		case ManageKey.ll_manage_salary_query:
			PopViewById(currentViewId);
			SwitchView(ManageKey.ll_manage_salary);
			break;
		case ManageKey.ll_manage_add_group:
		case ManageKey.ll_manage_group_info:
			OwerBaseView base = getOwerBaseView(currentViewId);
			boolean isRefresh = false;
			if(base != null)
			{
				isRefresh = base.isRefresh();
			}
			PopViewById(currentViewId);
			SwitchView(ManageKey.ll_manage_group);
			if(isRefresh)
			{
				 base = getOwerBaseView(currentViewId);
				 base.refashData();
			}
			break;
		case ManageKey.ll_manage_add_custom:
			PopViewById(currentViewId);
			SwitchView(R.id.ll_custom_personal_info);
			break;
		case ManageKey.ll_manage_custom_info:
		//case ManageKey.ll_manage_add_custom:
			PopViewById(currentViewId);
			SwitchView(ManageKey.ll_manage_custom);
			break;
		case ManageKey.ll_manage_add_custom_result:
			PopViewById(currentViewId);
			SwitchView(ManageKey.ll_manage_add_custom);
			break;
		case OwerViewKey.ll_personal_friend_detail:
		case OwerViewKey.ll_personal_add_friend:
			PopViewById(currentViewId);
			SwitchView(OwerViewKey.ll_personal_friend);
			break;
		case ManageKey.ll_manage_custom_message:
			PopViewById(currentViewId);
			SwitchView(ManageKey.ll_manage_custom);
			break;
		default:

			break;
		}
	}
	
//	protected void onTopRightClick()
//	{
//		if(ManageKey.ll_manage_custom == currentViewId)
//		{
//			SwitchView(ManageKey.ll_manage_add_custom);
//		}
//		else if(OwerViewKey.ll_personal_message == currentViewId)
//		{
//			doDeleteMessage();
//		}
//	}
	
	
	protected void sortCustomColumn(int index)
	{
		if(mManageCustomView != null)
		{
			mManageCustomView.sortCustomColumn(index);
		}
	}
	
	@Override
	protected OwerBaseView getOwerBaseView(int viewId)
	{
		OwerBaseView baseView = super.getOwerBaseView(viewId);
		if(baseView != null)
			return baseView;
		
		switch (viewId) {
		case R.id.ll_manage_personal_info:
			baseView = mMyManageView;
			break;
		case ManageKey.ll_manage_custom:
			baseView = mManageCustomView;
			break;
		case ManageKey.ll_manage_group:
			baseView = mManageGroupView;
			break;
		case ManageKey.ll_manage_salary:
			baseView = mManageSalaryView;
			break;
		case ManageKey.ll_manage_salary_query:
			baseView = mManageSalaryQueryView;
			break;
		case ManageKey.ll_manage_add_group:
			baseView = mManageAddGroupView;
			break;
		case ManageKey.ll_manage_group_info:
			baseView = mMangeGroupInfoView;
			break;
		case ManageKey.ll_manage_custom_info:
			baseView = mManageCustomInfoView;
			break;
		case ManageKey.ll_manage_add_custom:
			baseView = mManageAddCustomView;
			break;
		case ManageKey.ll_manage_add_custom_result:
			baseView = mManageAddCustomResultView;
			break;
		case ManageKey.ll_manage_custom_message:
			baseView = mManageMessageView;
			break;
		default:
		}
		
		return baseView;
	}
	
	public void SwitchSalaryQuery(int id, DateParams dinfo)
	{
		CreateSpView(R.layout.manage_personal_salary_query, id);
		if(mManageSalaryQueryView != null)
			mManageSalaryQueryView = null;
		mManageSalaryQueryView = new ManageSalaryQueryView(mContext, this, dinfo);
	}
	
	public void enterCustomView()
	{
		Integer t = 3;
		SwitchView(ManageKey.ll_manage_custom, t);
	}
	
	public void enterMessageView(String mobile, String userName)
	{
		String strUser = userName;
		if(StringUtil.isEmpty(strUser) || strUser.equals("null"))
		{
			strUser = mobile;
		}
		CustomInfo cinfo = new CustomInfo();
		cinfo.setName(strUser);
		cinfo.setTianYi(mobile);
		SwitchView(ManageKey.ll_manage_custom_message, cinfo);
	}
	
	protected void onTopRightClick()
	{
		super.onTopRightClick();
		if(R.id.ll_custom_personal_info == currentViewId)
		{
			mMyCustomView.customInfoShow();
		}
		else if(ManageKey.ll_manage_custom == currentViewId)
		{
			if(mManageCustomView != null)
			{
				mManageCustomView.sortCustom();
			}
			//SwitchView(ManageKey.ll_manage_add_custom);
		}
	}

	protected boolean CreateViewById(int Id, Object param)
	{	
		boolean isCreate = super.CreateViewById(Id, param);
		if(isCreate)
			return true;
		
		switch (Id) {
		case ManageKey.ll_manage_custom:
			addSubView(R.layout.manage_personal_custom, Id);
			if(mManageCustomView != null)
				mManageCustomView = null;
			mManageCustomView = new ManageCustomView(mContext, this, (Integer)param);
			break;
		case ManageKey.ll_manage_salary:
			addSubView(R.layout.manage_personal_salary, Id);
			if(mManageSalaryView != null)
				mManageSalaryView = null;
			mManageSalaryView = new ManageSalaryView(mContext, this);
			break;
		case ManageKey.ll_manage_group:
			addSubView(R.layout.manage_personal_group, Id);
			if(mManageGroupView != null)
				mManageGroupView = null;
			mManageGroupView = new ManageGroupView(mContext, this);
			break;
		case ManageKey.ll_manage_add_group:
			addSubView(R.layout.client_group_new_view, Id);
			if(mManageAddGroupView != null)
				mManageAddGroupView = null;
			mManageAddGroupView = new ManageAddGroupView(mContext, this, param);
			break;
		case ManageKey.ll_manage_group_info:
			addSubView(R.layout.client_group_list, Id);
			if(mMangeGroupInfoView != null)
				mMangeGroupInfoView = null;
			mMangeGroupInfoView = new MangeGroupInfoView(mContext, this, param);
			break;
		case ManageKey.ll_manage_custom_info:
			addSubView(R.layout.manage_personal_custom_info, Id);
			if(mManageCustomInfoView != null)
				mManageCustomInfoView = null;
			mManageCustomInfoView = new ManageCustomInfoView(mContext, this, param);
			break;
		case ManageKey.ll_manage_add_custom:
			addSubView(R.layout.manage_personal_add_custom, Id);
			if(mManageAddCustomView != null)
				mManageAddCustomView = null;
			mManageAddCustomView = new ManageAddCustomView(mContext, this);
			break;
		case ManageKey.ll_manage_add_custom_result:
			addSubView(R.layout.manage_personal_add_custom_success, Id);
			if(mManageAddCustomResultView != null)
				mManageAddCustomResultView = null;
			mManageAddCustomResultView = new ManageAddCustomResultView(mContext, this, param);
			break;
		case ManageKey.ll_manage_custom_message:
			addSubView(R.layout.manage_personal_message, Id);
			if(mManageMessageView != null)
				mManageMessageView = null;
			mManageMessageView = new ManageMessageView(mContext, this, param);
			break;
			
		}
		
		return true;
	}
}
